package com.wlw.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import com.wlw.*;
import com.wlw.entity.Remark;
import com.wlw.entity.Scenery;
import com.wlw.entity.User;
import com.wlw.service.RemarkService;
import com.wlw.service.SceneryService;

import edu.xjsoft.base.orm.support.Page;
import edu.xjsoft.base.orm.support.PropertyFilter;

@Controller
@RequestMapping("/remarkManage/*")
public class RemarkAction {
	
	@Autowired
	private RemarkService remarkService;
	@Autowired
	private SceneryService sceneryService;
	
	@RequestMapping("{pageNo}")
	public String list(@PathVariable int pageNo,Model model){
		Page<Remark> page = new Page<Remark>(5);
		page.setPageNo(pageNo);
		remarkService.loadAll(page);
		model.addAttribute("page", page);
		return "background/remarkManage";
	}
	@RequestMapping("delRe")
	public String delRe(HttpServletRequest req){
		int id = Integer.parseInt(req.getParameter("id"));
		remarkService.remove(id);
		return "background/successDel";
	}
	@RequestMapping("multiDel")
	public String multiDel(HttpServletRequest req){
		String name[] = req.getParameterValues("checkbox");
		for (String i : name) {
			int id = Integer.parseInt(i);
			remarkService.remove(id);
		}
		return "background/successDel";
	}
	
	@RequestMapping("remark")
	public String remark(HttpServletRequest req){
		User user = (User) req.getSession().getAttribute("user");
		int id = Integer.parseInt(req.getParameter("id"));
		PropertyFilter filters = new PropertyFilter("id", id);
		List<Scenery> sceneries = sceneryService.search(filters);
		Remark remark = new Remark();
		int mark = 0;
		if (user!=null) {
			String markStr = req.getParameter("mark");
			if (markStr.equals("")) {
				mark = 5;
			}else {
				mark = Integer.parseInt(markStr);
			}
			String content = req.getParameter("content");
			remark.setContent(content);
			remark.setMark(mark);
			remark.setUser(user);
			remark.setScenery(sceneries.get(0));
			remarkService.saveOrUpdate(remark);
		}
		return "foreground/index";
	}
	
}
